package com.gingko.duominuo.ui.fragment

import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RadioButton
import androidx.core.content.ContextCompat
import com.gingko.duominuo.R
import com.gingko.duominuo.databinding.FragmentFailureReasonBottomDialogBinding
import com.gingko.duominuo.utils.dp2px
import com.gingko.duominuo.utils.singleClick
import com.gingko.duominuo.utils.toast
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import java.util.*

class FailureReasonBottomDialogFragment : BottomSheetDialogFragment() {
    private val mViewBinding by lazy { FragmentFailureReasonBottomDialogBinding.inflate(layoutInflater) }
    private val failureReasons by lazy { mutableListOf<String>() }
    private var refuseReason: String? = null
    var callback: Callback? = null

    companion object {
        private const val ARG_FAILURE_REASON = "failureReason"

        @JvmStatic
        fun newInstance(failureReason: List<String>) = FailureReasonBottomDialogFragment().apply {
            arguments = Bundle().apply {
                putStringArrayList(ARG_FAILURE_REASON, failureReason as ArrayList<String>)
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme)
        failureReasons.addAll(arguments?.getStringArrayList(ARG_FAILURE_REASON) ?: listOf<String>())
        refuseReason = failureReasons[0]
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return mViewBinding.root

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        failureReasons.forEachIndexed { index, data ->
            val radioButton = RadioButton(requireContext())
            radioButton.buttonDrawable = ContextCompat.getDrawable(requireContext(), R.drawable.selector_radio_default)
            radioButton.gravity = Gravity.TOP
            radioButton.setPadding(dp2px(10), 0, 0, 0)
            radioButton.includeFontPadding = false
            radioButton.text = data
            radioButton.id = View.generateViewId()
            if (index != 0) {
                val layoutParams = ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                layoutParams.topMargin = dp2px(25)
                radioButton.layoutParams = layoutParams
            } else {
                radioButton.isChecked = true
            }
            mViewBinding.rgReason.addView(radioButton)
        }
        mViewBinding.rgReason.setOnCheckedChangeListener { radioGroup, i ->
            refuseReason = radioGroup.findViewById<RadioButton>(i).text.toString()
        }
        mViewBinding.tvConfitm.singleClick {
            refuseReason?.let {
                callback?.onConfirm(it)
                dismissAllowingStateLoss()
            } ?: "请选择一个拒绝原因".toast()
        }
        mViewBinding.tvCancel.singleClick {
            dismissAllowingStateLoss()
        }


    }

    interface Callback {
        fun onConfirm(reason: String)
    }
}